import { nextTick, ref } from "vue"
import { num3, round2 } from '@/utils/num'


import type { NetRowProps } from './NetRow.vue'
import type { NetRow } from './types'


export const initNet = (props: NetRowProps): NetRow => {
  const { index, baseForm, lastBuyTrigger, lastRatio } = props
  const { baseTrigger, gap, baseCost } = baseForm
  const ratio = index ? num3(lastRatio).subtract(0.05).value : 1
  const buyTrigger = +(baseTrigger * ratio).toFixed(3)
  const buyPrice = +(buyTrigger - gap).toFixed(3)
  const buyCost = baseCost * (2 - ratio)
  const buyNum = round2(buyCost / buyPrice)
  const sellTrigger = +(lastBuyTrigger || buyTrigger * 1.05).toFixed(3)
  const sellPrice = +(sellTrigger + gap).toFixed(3)
  const sellNum = round2(getSellRatio(ratio) * buyNum)

  return {
    ratio,
    type: 'small',
    buyTrigger,
    buyPrice,
    buyNum,
    buyCost,
    sellTrigger,
    sellPrice,
    sellNum
  }
}

export const getSellRatio = (ratio: number) => {
  return num3(ratio - 0.2).value > 0.5 ? num3(ratio - 0.2).value : 0.5
}


export const useNetRow = (props: NetRowProps) => {
  const { index, baseForm } = props
  const { gap } = baseForm
  const net = ref<NetRow>(initNet(props))

  const setBuyTrigger = () => {
    const { ratio } = net.value
    const { baseTrigger } = props.baseForm

    net.value.buyTrigger = num3(baseTrigger).multiply(ratio).value
  }

  const setBuyPrice = () => {
    const { buyTrigger } = net.value
    net.value.buyPrice = num3(buyTrigger).subtract(gap).value
  }

  const handleRatioChange = () => {
    console.log('档位系数变了');
    setBuyTrigger()
    setBuyPrice()
    setBuyCost()
    setBuyNum()
    setSellNum()
  }

  const handleBuyTriggerChange = () => {
    console.log('买入触发价变了');
    setRatio()
    handleRatioChange()
  }

  const handleBaseTriggerChange = () => {
    handleRatioChange()
    nextTick(() => {
      setSellTrigger()
      setSellPrice()
    })
  }

  const handleBaseCostChange = () => {
    setBuyCost()
    setBuyNum()
    setSellNum()
  }

  const setRatio = () => {
    const { buyTrigger } = net.value
    const { baseTrigger } = props.baseForm

    net.value.ratio = +(buyTrigger / baseTrigger).toFixed(2)
  }

  const setBuyCost = () => {
    const { ratio } = net.value
    const { baseCost } = props.baseForm
    const res = num3(baseCost).multiply(2 - ratio)
    net.value.buyCost = res.value
  }

  const setBuyNum = () => {
    const { buyCost, buyPrice } = net.value
    const num = num3(buyCost).divide(buyPrice).value
    net.value.buyNum = round2(num)
  }

  const setSellNum = () => {
    const { buyNum, ratio } = net.value
    const sellRatio = getSellRatio(ratio)
    const num = num3(buyNum).multiply(sellRatio).value
    net.value.sellNum = round2(num)
  }

  const setSellTrigger = () => {
    if (!index) {
      const { baseTrigger } = props.baseForm
      net.value.sellTrigger = num3(baseTrigger).multiply(1.05).value
    } else {
      net.value.sellTrigger = props.lastBuyTrigger
    }
  }

  const setSellPrice = () => {
    const { sellTrigger } = net.value
    net.value.sellPrice = num3(sellTrigger).add(gap).value
  }


  return {
    net,
    handleRatioChange,
    handleBuyTriggerChange,
    handleBaseTriggerChange,
    handleBaseCostChange
  }
}